var captcha= (function() {
	var imgurl="/userCenter/imageCode",obj,phone,imgOk;
	return {
		init: function(obj1,phone1,imgOk1) {
			obj=obj1;phone=phone1;imgOk=imgOk1;
			var _html = '<div id="captcha-bg" class="captcha-bg" tabindex="0"></div><div id="ui-popup" class="ui-popup"  tabindex="-1">';
			_html+='<div class="ui-dialog"><div class="ui-dialog-arrow-a"></div><div class="ui-dialog-arrow-b"></div><table class="ui-dialog-grid"><tbody><tr><td class="ui-dialog-body"><div class="ui-dialog-content">';
			_html+='<img id="captcha-img" class="captcha-img" src="'+imgurl+'"><input type="button" class="captcha-btn" id="captcha-btn" value="刷新"/><input id="captcha-input" class="captcha-input" type="text"><input class="captcha-post" id="captcha-post" type="button" value="确定"/></div></td></tr></tbody></table></div></div>';
			$('body').append(_html);
			var left=parseInt(obj.offset().left);
			var top=parseInt(obj.offset().top);
			var width=parseInt(obj.width());
			$(".ui-popup").css({"left":(left+width/2-110+"px"),"top":(top-120)+"px"});
			captcha.refresh();
			$('#captcha-img').on('click', function() {
				captcha.refresh();
			});
			$('#captcha-btn').on('click', function() {
				captcha.refresh();
			});
			$('#captcha-post').on('click', function() {
				captcha.postCode();
			});
			$("#captcha-bg").on('click',function(){
				captcha.close();
			});
		},
		refresh: function() {
			$("#captcha-img").attr("src",imgurl+"?t="+new Date().getTime());
			var code=$("#captcha-input").val("");
		},
		postCode: function(){
			var code=$("#captcha-input").val();
			var phoneNumber=phone;
			$.ajax({
				type:"POST",
				url:"/userCenter/sendMobileValidCode",
				data:{code:code,phoneNumber:phoneNumber},
				dataType:"json",
				success: function(data){
					if(data.result==1){
						imgOk();
						captcha.close();
					}else{
						clickautohide("出错了:"+data.message,3);
						captcha.refresh();
					}
				}
			});
		},
		close: function(){
			$('#captcha-bg,#ui-popup').remove();
		},	
	};
})();

